package cn.tsmchina.one.controller.order;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.tsmchina.one.domain.category.Category;
import cn.tsmchina.one.domain.config.Config;
import cn.tsmchina.one.domain.user.Customer;
import cn.tsmchina.one.domain.user.User;
import cn.tsmchina.one.pojo.RestaurantPojo;
import cn.tsmchina.one.service.category.CategoryService;
import cn.tsmchina.one.service.config.ConfigService;
import cn.tsmchina.one.service.order.OrderService;
import cn.tsmchina.one.service.user.CustomerService;
import cn.tsmchina.one.util.C;
import cn.tsmchina.one.util.ConfigUtil;
import cn.tsmchina.one.util.JsonUtil;
import cn.tsmchina.one.util.LoginUtil;
import cn.tsmchina.one.util.PayParams;

@Controller
//@RequestMapping("/orderm")
public class OrdermController
{
	@Autowired
	CategoryService categoryService;
	
	@Autowired
	OrderService orderService;
	
	@Autowired
	CustomerService customerService;
	
	@Autowired
	ConfigService configService;
	
	//微信支付必须存在于一级目录下！！
	@RequestMapping("/orderminfo")
	public String orderInfo(String shopInfo, double totalPrice, int pcount,int cid, HttpServletRequest request) throws UnsupportedEncodingException
	{
		User user = (User) request.getSession().getAttribute("user");
		if(user!=null) {
			request.setAttribute("host", "http://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath());
			request.setAttribute("user", user);
			request.setAttribute("credits", user.getCredits());
			request.setAttribute("totalPrice", totalPrice);
			request.setAttribute("pcount", pcount);
			shopInfo = java.net.URLDecoder.decode(shopInfo, "utf-8");
			request.setAttribute("shopInfo", shopInfo);
			/*查看店铺地址*/
			List<Category> cs = categoryService.getAllSubRestaurant(cid);
			String raddress = "未知";
			for(Category c : cs){
				if(c.getName().equalsIgnoreCase(shopInfo)){
					String params = c.getParams();
					RestaurantPojo pojo = JsonUtil.parseObject(RestaurantPojo.class, params);
					raddress = pojo.getAddress();
					break;
				}
			}
			request.setAttribute("raddress", raddress);
			/**/
		}
		
		//微信支付添加
		Config wcpay = configService.getConfig(1, C.ConfigKeys.WC_PAY);
		if(null!=wcpay){
			request.setAttribute("wcpay", "yes");
			request.setAttribute("wcpayNotifyUrl", ConfigUtil.get("wcpayNotifyUrl"));
			
			PayParams wxpay = JsonUtil.parseObject(PayParams.class, wcpay.getConfigValue());
			request.setAttribute("wxpay", wxpay);
		}
		
		return "m/orderInfo";
	}
	
	@RequestMapping("/orderm/repay/{id}")
	public String orderInfo(@PathVariable("id")int id, HttpServletRequest request) throws UnsupportedEncodingException
	{
		User user = (User) request.getSession().getAttribute("user");
		if(user!=null) {
			request.setAttribute("host", "http://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath());
			request.setAttribute("user", user);
			request.setAttribute("credits", user.getCredits());
//			request.setAttribute("totalPrice", totalPrice);
//			request.setAttribute("pcount", pcount);
//			shopInfo = java.net.URLDecoder.decode(shopInfo, "utf-8");
//			request.setAttribute("shopInfo", shopInfo);

		}
		return "m/orderInfo";
	}
	
	@RequestMapping("/orderm/delete/{id}")
	public String deleteOrder(@PathVariable("id") long id, HttpServletRequest request)
	{
		/*User user = (User) request.getSession().getAttribute("user");
		if(user!=null) {
			request.setAttribute("host", "http://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath());
			request.setAttribute("user", user);
			request.setAttribute("credits", user.getCredits());
			//TODO根据积分兑换规则计算出新的价格
			request.setAttribute("totalPrice", totalPrice);
			request.setAttribute("pcount", pcount);
			Object rid = request.getSession().getAttribute("currentRestaurantId");
			if(rid!=null) {
				Category shopCategory = categoryService.getById(rid.toString());
				request.setAttribute("shopInfo", shopCategory.getName());
			}
		}*/
		User user = (User) request.getSession().getAttribute("user");
		Customer customer = customerService.getCustomerById(user.getCustomerId());
		orderService.deleteOrder(id);
		/*Long userId = (Long) request.getSession().getAttribute("userId");
		List<Order> olist = orderService.getUserOrders(userId);
		request.setAttribute("orders", olist);*/
		return "redirect:/user/backend?cid="+customer.getLoginId();
	}
}
